package top.hzy520.user.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.hzy520.common.constants.Constants;
import top.hzy520.common.exception.YuyuBuyException;
import top.hzy520.common.resp.ApiResult;
import top.hzy520.common.rest.BaseController;
import top.hzy520.user.dto.ModifyPawwordInputDTO;
import top.hzy520.user.entity.RegisterInfo;
import top.hzy520.user.entity.User;
import top.hzy520.user.entity.UserElement;
import top.hzy520.user.service.UserService;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 22:05 2018/5/30
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
       @Autowired
       @Qualifier("userServiceImpl")
      private UserService userService;
       /**
       *@Author: HouZeYu
       *@Description: 短信注册
       *@Date: 17:22 2018/6/2
       */
       @RequestMapping("/smsRegister")
       public ApiResult smsRegister(@RequestBody RegisterInfo registerInfo) throws Exception {
           userService.smsRegister(registerInfo);
           return new ApiResult(Constants.RESP_STATUS_OK,"注册成功");
       }
       /**
       *@Author: HouZeYu
       *@Description: 邮箱注册
       *@Date: 17:23 2018/6/2
       */
    @RequestMapping("/emailRegister")
    public ApiResult emailRegister(@RequestBody RegisterInfo registerInfo) throws Exception {
        userService.emailRegister(registerInfo);
        return new ApiResult(Constants.RESP_STATUS_OK,"注册成功");
    }
        /**
        *@Author: HouZeYu
        *@Description: 手机账号登录
        *@Date: 12:25 2018/6/3
        */
    @RequestMapping("/smsLogin")
    public ApiResult mobileLogin(@RequestBody @Valid User user, HttpSession session){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"登录成功");
     UserElement userElement=userService.mobileLogin(user);
     if (userElement!=null){
        if (session.getAttribute(Constants.REQUEST_USER_SESSION)==null){
            session.setAttribute(Constants.REQUEST_USER_SESSION,userElement);
        }
        apiResult.setData(userElement);
     }
     return apiResult;
    }
    /**
    *@Author: HouZeYu
    *@Description:  邮箱账号登录
    *@Date: 15:30 2018/6/3
    */
    @RequestMapping("/emailLogin")
     public ApiResult emailLogin(@RequestBody @Valid User user, HttpSession session){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"登录成功");
     UserElement userElement=userService.emailLogin(user);
     if (userElement!=null){
         if (session.getAttribute(Constants.REQUEST_USER_SESSION)==null){
          session.setAttribute(Constants.REQUEST_USER_SESSION,userElement);
         }
              apiResult.setData(userElement);
     }
     return apiResult;
    }

    /**
    *@Author: HouZeYu
    *@Description: 查询用户信息
    *@Date: 13:14 2018/6/3
    */
    @RequestMapping("/getUserInfo")
    public ApiResult getUserInfo(HttpSession session){
      UserElement userElement= (UserElement)session.getAttribute(Constants.REQUEST_USER_SESSION);
      ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"查询成功");
      if (userElement==null){
          throw new YuyuBuyException("请求头错误");
      }
          apiResult.setData(userElement);
      return apiResult;
    }
    /**
    *@Author: HouZeYu
    *@Description: 用户退出
    *@Date: 13:33 2018/6/3
    */
    @RequestMapping("/logout")
    public ApiResult logOut(HttpSession session){
          session.removeAttribute(Constants.REQUEST_USER_SESSION);
          return new ApiResult(Constants.RESP_STATUS_OK,"退出成功");
    }
    /**
    *@Author: HouZeYu
    *@Description: 编辑昵称
    *@Date: 15:37 2018/6/3
    */
    @RequestMapping("/editUserInfo")
    public ApiResult editUserInfo(@RequestBody @Valid User user,HttpSession session){
         UserElement userElement=userService.editUserInfo(user,getCurrentUser(session));
         if (session.getAttribute(Constants.REQUEST_USER_SESSION)!=null){
             session.setAttribute(Constants.REQUEST_USER_SESSION,userElement);
         }
        return new ApiResult(Constants.RESP_STATUS_OK,"修改成功");
    }
    /**
    *@Author: HouZeYu
    *@Description: 上传头像
    *@Date: 19:06 2018/6/3
    */
    @RequestMapping("/uploadHeadImage")
    public ApiResult uploadHeadImage(@RequestParam(name = "file",required = false) MultipartFile file,HttpSession session) throws IOException {
        ApiResult result=new ApiResult(Constants.RESP_STATUS_OK,"上传成功");
        UserElement userElement=userService.uploadHeadImage(file,getCurrentUser(session));
        if (session.getAttribute(Constants.REQUEST_USER_SESSION)!=null){
            session.setAttribute(Constants.REQUEST_USER_SESSION,userElement);
            result.setData(userElement);
        }
        return result;
    }
    /**
    *@Author: HouZeYu
    *@Description: 修改密码
    *@Date: 18:57 2018/6/4
    */
    @RequestMapping("/modifyPassword")
    public ApiResult modifyPassword(@RequestBody ModifyPawwordInputDTO modifyPawwordInputDTO,HttpSession session){
        userService.modifyPassword(modifyPawwordInputDTO,getCurrentUser(session));
       return new ApiResult(Constants.RESP_STATUS_OK,"修改成功");
    }
}
